package cn.edu.scau.cmi.chenyipeng.singleton;


import java.util.ArrayList;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class SingletonController {
	
	private int flag = 0;
	private Chairman chairman;
	private ArrayList<Marshal> ten_Marshal = new ArrayList<Marshal>();
	
    @FXML
    private Button singletonButton;

    @FXML
    private Button MarshalButton;

    @FXML
    private TextField inputNameTF;

    @FXML
    private Label emptyLabel;

    @FXML
    private Label showLabel;

    @FXML
    private Label nameLabel;

    @FXML
    void SingletonCreate(ActionEvent event) {
    	showLabel.setWrapText(true);
    	if(flag!=0) {
    		flag = 0;
    		showLabel.setText("");
    	}
    	if(chairman == null && inputNameTF.getText()!="") {
    		chairman = Chairman.getInstance(inputNameTF.getText());
    		showLabel.setText(showLabel.getText()+"您新创建的对象为"+chairman.toString());
    	}
    	else if(chairman!=null){
    		showLabel.setText(showLabel.getText()+
    				"\n您想创建名字为"+inputNameTF.getText()+
    				"的对象，但已经创建了这个类的单例,不能创建新的对象了，你现在获取的对象是"+chairman.toString());
    		
    	}
    	else {
    		showLabel.setText("主席姓名请不要为空！");
    	}
    	
    }

    @FXML
    void MarshalCreate(ActionEvent event) {
    	if(flag!=1) {
    		flag = 1;
    		showLabel.setText("");
    	}
    	showLabel.setWrapText(true);
    	if(Marshal.getCount() <10 && inputNameTF.getText()!="") {
    		ten_Marshal.add(Marshal.getInstance(inputNameTF.getText()));
    		showLabel.setText(showLabel.getText()+"\n您新创建的对象为"
    		+ten_Marshal.get(Marshal.getCount()-1).toString());
    	}
    	else if(Marshal.getCount()>=10){
    		showLabel.setText(showLabel.getText()+"\n多例模式已到达创建上限");
    	}
    	else {
    		showLabel.setText("主席姓名请不要为空！");
    	}
    }

}
